package Client;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Transfer 
{
		
	static void send(String i, String grp,String path,String fname) throws IOException
	{
		int port=15001;
		InetAddress ipad = InetAddress.getByName(i);		 

		System.out.println("Starting Transfer...");
		Socket sock = new Socket(ipad,port);
	
		PrintWriter outc = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
		
		outc.println(fname);
		outc.flush();
		outc.println(grp);
		outc.flush();
		//BufferedReader inc  = new BufferedReader (new InputStreamReader(sock.getInputStream()));
		//String ans = inc.readLine();
		//inc.close();
		//System.out.println(ans);
		//if(ans.compareTo("send")==0)
		//{		
		//String path = "C:\\Users\\Sudip Mittal\\workspace\\Project\\";
		//path+=dir;
		System.out.println(path);
		File myFile = new File(path);
		byte [] mybytearray  = new byte [(int)myFile.length()];
		System.out.println((int)myFile.length());
		FileInputStream fis = new FileInputStream(myFile);
		BufferedInputStream bis = new BufferedInputStream(fis);
		bis.read(mybytearray,0,mybytearray.length);
		OutputStream os = sock.getOutputStream();
		System.out.println("Sending...");
	//	    System.out.println(bis.read(mybytearray,0,mybytearray.length));
		os.write(mybytearray,0,mybytearray.length);
		os.flush();
		sock.close();  
		bis.close();
		os.close();
		outc.close();    
		System.out.println("-Completed-");
	    //}
	}
	
	static void read() throws IOException
	{
		BufferedReader ip_ad = new BufferedReader(new FileReader("./src/list_sync_ip.txt"));
		String temp;
		String K;
		String IP,g = null;
		String [] Files = null;
		
		while ((temp=ip_ad.readLine()).compareTo("a")!=0)
		{
			System.out.println("temp"  + temp);
			if (temp.contains(".")&& temp.compareTo("0.0.0.0")!=0)
			{
				IP=temp;
				int len = Files.length;
				int i=0;
				
				while(i<len)
				{
					K = ".\\" + g + "\\" + Files[i];
					send(IP,g,K,Files[i]);
					i++;
				}
	
			}
			else 
				{
					g=temp;
					Files=list_the_folder(g);
				}
		
		}

		
	}
	private static String [] list_the_folder(String gr) 
	{
		int gr_ids=Integer.parseInt(gr);
		 String path = ".\\";
			  String path1=path;
			  System.out.println("---------------------");
			  System.out.println(gr_ids);
			  path1=path1+String.valueOf(gr_ids);
			  System.out.println(path1);
			  String files;
			  File folder = new File(path1);
			  File[] listOfFiles = folder.listFiles(); 
			  String [] K = new String[listOfFiles.length];
			  for (int i = 0; i < listOfFiles.length; i++) 
			  {
				  if (listOfFiles[i].isFile()) 
				  {
					  files = listOfFiles[i].getName();
					  System.out.println(files);
					  K[i]=files.toString();
				  }
			  }
		return K;
	}

	public static void main (String [] args ) throws IOException 
	{
		read();
	}
}
/*
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Transfer 
{	
	static void send(String i, String grp,String path,String fname) throws IOException
	{
		int port=15000;
		InetAddress ipad = InetAddress.getByName(i);		 


		System.out.println("Starting Transfer...");
		Socket sock = new Socket(ipad,port);
	
		PrintWriter outc = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
		
		outc.println(fname);
		outc.flush();
		outc.println(grp);
		outc.flush();
		//BufferedReader inc  = new BufferedReader (new InputStreamReader(sock.getInputStream()));
		//String ans = inc.readLine();
		//inc.close();
		//System.out.println(ans);
		//if(ans.compareTo("send")==0)
		//{
		
			//String path = "C:\\Users\\Sudip Mittal\\workspace\\Project\\";
			//path+=dir;
			File myFile = new File(path);
			byte [] mybytearray  = new byte [(int)myFile.length()];
		    System.out.println((int)myFile.length());
		    FileInputStream fis = new FileInputStream(myFile);
		    BufferedInputStream bis = new BufferedInputStream(fis);
		    bis.read(mybytearray,0,mybytearray.length);
		    OutputStream os = sock.getOutputStream();
		    System.out.println("Sending...");
	//	    System.out.println(bis.read(mybytearray,0,mybytearray.length));
		    os.write(mybytearray,0,mybytearray.length);
		    os.flush();
		    //bis.close();
		    os.close();
		    outc.close();
	
		    sock.close();
		    System.out.println("-Completed-");
	    //}
	}
	
	static void read() throws IOException
	{
		System.out.println("transfer read invoked");
		BufferedReader ip_ad = new BufferedReader(new FileReader("./src/list_sync_ip.txt"));
		String temp;
		String K;
		String IP,g = null;
		String [] Files = null;
		
		while ((temp=ip_ad.readLine()).compareTo("a")!=0)
		{
			
			if (temp.contains("."))
			{
				System.out.println("sending to IP" + temp);
				IP=temp;
				int len = Files.length;
				System.out.println(Files.length);
				int i=0;
				
				while(i<len)
				{
					K = ".\\" + g + "\\" + Files[i];
					System.out.println(K + "   " + Files[i]);
					send(IP,g,K,Files[i]);
					i++;
				}
	
			}
			else 
				{
					g=temp;
					Files=list_the_folder(g);
				}
		
		}

		
	}
	private static String [] list_the_folder(String gr) 
	{
		int gr_ids=Integer.parseInt(gr);
		 String path = ".\\";
			  String path1=path;
			  System.out.println("---------------------");
			  System.out.println(gr_ids);
			  path1=path1+String.valueOf(gr_ids);
			  System.out.println(path1);
			  String files;
			  File folder = new File(path1);
			  File[] listOfFiles = folder.listFiles(); 
			  String [] K = new String[listOfFiles.length];
			  for (int i = 0; i < listOfFiles.length; i++) 
			  {
				  if (listOfFiles[i].isFile()) 
				  {
					  files = listOfFiles[i].getName();
					  System.out.println(files);
					  K[i]=files.toString();
				  }
			  }
		return K;
	}

	public static void main (String [] args ) throws IOException 
	{
		read();
	}
}*/